package sv.com.renacempleo.web.security;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;

import sv.com.renacempleo.exception.WebException;
import sv.com.renacempleo.web.util.DBContext;

public class CustomJdbcDaoImpl extends JdbcDaoImpl {
	private static final Logger logger = Logger.getLogger(CustomJdbcDaoImpl.class);
	
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		
		UserDetails user = super.loadUserByUsername(username);
		CustomUser customUser = new CustomUser(
				user.getUsername(), 
				user.getPassword(), 
				user.isEnabled(),user.isAccountNonExpired(), 
				user.isCredentialsNonExpired(), 
				user.isAccountNonLocked(), 
				user.getAuthorities());
		try {
			DBContext.getSgdUsuarioDao().fillCustomUser(customUser, user.getUsername());
		} catch (WebException e) {
			logger.error("Exception: ", e);
			throw new UsernameNotFoundException(e.getExceptionMessage());
		}
		return customUser;
	}
	
	
}
